#!/bin/bash
SRCS=
DST=
LOG_FILE=
INCLUDES=
LINKS=
FLAGS=
RUN_ARGS=()
readonly TEMPLATE_NAME=dmake.t
readonly TEMPLATE_FILE='#!/bin/bash\nSRCS=`ls ./*.cc`\nDST=./a.out\nLOG_FILE=./a.log\n#INCLUDES=`pkg-config --cflags gtk+-3.0`\nINCLUDES=''\n#LINKS=`pkg-config --libs gtk+-3.0`\nLINKS=''\nFLAGS=''\nRUN_ARGS=()'

_CMD=
_SHOW=
_TARGET=
_DEBUG=
_BUILD_OK_=
function is_cmd() {
    local iscmd=0
    case $1 in
        # had param
        b|r|t|br)
        iscmd=1;;
        # no param
        -h)
        iscmd=2;;
        # sub cmd
        -v|-d)
        iscmd=3;;
    esac
    return $iscmd
}
function parser_cmd() {
    local show=0
    local cmd=-h
    local target=
    local debug=0
    for ((i=1;i<=$#;)); do
        is_cmd $1
        local iscmd=$?

        # had param, parser next
        if [ $iscmd == 1 ]; then
            cmd=$1
            is_cmd $2
            local iscmd=$?
            if [ $iscmd == 0 ]; then
                target=$2
                shift
            fi
        elif [ $iscmd == 2 ]; then
            cmd=$1
            break
        elif [ $iscmd == 3 ]; then
            if [ $1 == -v ]; then
                show=1
            elif [ $1 == -d ]; then
                debug=1
            fi
        fi

        shift
    done

    _CMD=$cmd
    _SHOW=$show
    _TARGET=$target
    _DEBUG=$debug
}

function help() {
    echo Usage: dmake [actions] [options]
    echo Actions:
    echo -e "\t" t "\t"  Generate a script
    echo -e "\t" b "\t"  Build all files
    echo -e "\t" r "\t"  Run the app
    echo -e "\t" br "\t" Build all files and Run the app
    echo Options:
    echo -e "\t" -v "\t" Display details
    echo -e "\t" -d "\t" Debug mode
    echo -e "\t" -h "\t" Show this help
}

function build() {
    _BUILD_OK_=0
    local debug=
    if [ $2 == 1 ]; then
        debug='-O0 -g'
    fi
    local cmd="g++ $FLAGS $INCLUDES $SRCS -o $3 $LINKS $debug"

    if [ $1 == 1 ]; then
        echo 'build command:'
        echo "$cmd"
        echo '...building...'
    fi

    `$cmd`
    
    if [ $? -eq 0 ]; then
        echo 'Done!'
        _BUILD_OK_=1
    fi
}

function run() {
    local cmd="./$2 ${RUN_ARGS[@]}"
    if [ $1 == 1 ]; then
        echo 'run command:'
        echo "$cmd"
        echo '...runing...'
    fi

    if [ "$LOG_FILE" = "" ]; then
        echo `$cmd`
    else
        `$cmd >> $LOG_FILE`
    fi

    echo exist with code $?
}

function template() {
    echo -e $TEMPLATE_FILE > $2
}

function run_script() {
    local script=$1
    if [ -f $script ]; then
        source $script
    fi
}

function run_check() {
    local dname=`dirname $DST`
    if [ ! -d $dname ]; then
        mkdir -p $dname
    fi
}

function init() {
    run_script $TEMPLATE_NAME

    parser_cmd $*
    local cmd=$_CMD
    local show=$_SHOW
    local target=$DST
    local debug=$_DEBUG

    if [ $cmd == b ]; then
        run_check
        build $show $debug $target
    elif [ $cmd == r ]; then
        run_check
        run $show $target
    elif [ $cmd == t ]; then
        template $show $TEMPLATE_NAME
    elif [ $cmd == br ]; then
        run_check
        build $show $debug $target
        if [ $_BUILD_OK_ == 1 ]; then
            run $show $target
        fi
    elif [ $cmd == -h ]; then
        help $show $target
    fi
}

init $*